package com.example.lucky.draw.server.job.config;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.step.tasklet.TaskletStep;
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class HelloJobConfig {

    /**
     * 任务执行器
     * 用于启动job
     */
    @Autowired
    private JobLauncher jobLauncher;

    /**
     * Job 构建工厂
     * 构建job对象
     */
    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    /**
     * step 构建工厂
     * 构建step对象
     */
    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    /**
     * 构造job对象
     */
    @Bean
    public Job job(){
        Job helloJob = jobBuilderFactory.get("helloJob")
                .start(step())//指定job的起始step
                .build();
        return helloJob;
    }

    /**
     * 构造step对象
     */
    @Bean
    public Step step(){
        TaskletStep step = stepBuilderFactory.get("helloStep")
                //指定step执行逻辑，类似于Thread.run()方法
                .tasklet((contribution, chunkContext) -> {
                    //打印hello world
                    System.out.println("hello world");
                    //返回RepeatStatus.FINISHED，表示step执行完成
                    return RepeatStatus.FINISHED;
                }).build();
        return step;
    }


}
